package com.ztc.AppUser.service;


import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.dao.AttachmentDao;
import com.aqgj.SysArea.bean.SysArea;
import com.aqgj.SysArea.dao.SysAreaDao;
import com.aqgj.util.JsonWrapper;
import com.aqgj.util.Pager;
import com.ztc.AppUser.bean.AppUser;
import com.ztc.AppUser.dao.AppUserDao;
import com.ztc.ZtcCapitalIncome.bean.ZtcCapitalIncome;
import com.ztc.ZtcCar.dao.ZtcCarDao;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.dao.ZtcTransportDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("AppUser")
public class AppUserManager{
    @Autowired
    AppUserDao appUserDao;
    @Autowired
    AttachmentDao attachmentDao;
    @Autowired
    ZtcTransportDao ztcTransportDao;

    public AppUser get(String zid) {
        return appUserDao.get(zid);
    }

    public AppUser findUniqueBy(String propertyName, Object value) {
        return appUserDao.findUniqueBy(propertyName, value);
    }

    public List<AppUser> findBy(String propertyName1, Object value1, String propertyName2, Object value2) {
        return appUserDao.findBy(propertyName1, value1, propertyName2, value2);
    }

    public List<AppUser> findBy(String propertyName, Object value) {
        return appUserDao.findBy(propertyName, value);
    }

    /**
     * 获取用户列表.
     * @param pageNo    页 码
     * @param pageSize 每页数量
     * @param search    搜索条件
     * @param distinguishType  类别（1 除待审核数据，2待审核数据）
     * @param isExpress  类别（0 顺风车，1快递员）
     * @return
     */
    public HashMap<String,Object> getPrivateCarUserListByType(Integer pageNo,Integer pageSize,String search,Integer distinguishType,Integer isExpress,String cid){
        Pager<List<Map>> appUserPager = appUserDao.getPrivateCarUserListByType(pageNo, pageSize, search, distinguishType,isExpress,cid);
        return JsonWrapper.successPagerWrapper(appUserPager.getContent(),appUserPager.getRecordTotal());
    }

    public Map getUserInfo(AppUser user) {
        Map result = appUserDao.getUserInfo(user.getId());
        if(user.getType()==3||user.getType()==4||user.getExpStatus()==1||user.getExpStatus()==2){//快递员
            ZtcTransport ztcTransport=ztcTransportDao.get(user.getTransportId());
            if(ztcTransport!=null){
                result.put("company",ztcTransport.getFullName());
            }else{
                result.put("company","");
            }
        }
        if (result != null && result.size() > 0) {
            List<Attachment> attachmentList = attachmentDao.findBy("businessId", user.getId());
            if (attachmentList.size() > 0) {
                for (Attachment a : attachmentList) {
                    //车主正照
                    if ("driver_img".equals(a.getWorkItem())) {
                        result.put("driverImgPath", a.getId());
                        result.put("driverImgFileName", a.getFileName());
                        continue;
                    }
                    //驾驶照
                    if ("driver_lic_img".equals(a.getWorkItem())) {
                        result.put("driverLicImgPath", a.getId());
                        result.put("driverLicImgFileName", a.getFileName());
                        continue;
                    }
                    //快递员正面照
                    if ("expressImg".equals(a.getWorkItem())) {
                        result.put("expressImgPath", a.getId());
                        result.put("expressImgFileName", a.getFileName());
                        continue;
                    }
                    //手持身份证正面照
                    if ("cardImg".equals(a.getWorkItem())) {
                        result.put("cardImgPath", a.getId());
                        result.put("cardImgFileName", a.getFileName());
                        continue;
                    }
                    //身份证正面照
                    if ("cardFaceImg".equals(a.getWorkItem())) {
                        result.put("cardFaceImgPath", a.getId());
                        result.put("cardFaceImgFileName", a.getFileName());
                        continue;
                    }
                    //身份证反面照
                    if ("cardConImg".equals(a.getWorkItem())) {
                        result.put("cardConImgPath", a.getId());
                        result.put("cardConImgFileName", a.getFileName());
                    }
                }
            } else {
                result.put("driverImgPath", "");
                result.put("driverImgFileName", "");
                result.put("driverLicImgPath", "");
                result.put("driverLicImgFileName", "");
            }
            if (result.get("cid") != null) {
                List<Attachment> attachmentList1 = attachmentDao.findBy("businessId", result.get("cid").toString());
                if (attachmentList1.size() > 0) {
                    for (Attachment a : attachmentList1) {
                        //车照
                        if ("car_img".equals(a.getWorkItem())) {
                            result.put("carImgPath", a.getId());
                            result.put("carImgFileName", a.getFileName());
                            continue;
                        }
                        //行驶证照
                        if ("car_work_img".equals(a.getWorkItem())) {
                            result.put("carWorkImgPath", a.getId());
                            result.put("carWorkImgFileName", a.getFileName());
                        }
                    }
                } else {
                    result.put("carImgPath", "");
                    result.put("carImgFileName", "");
                    result.put("carWorkImgPath", "");
                    result.put("carWorkImgFileName", "");
                }
            }
        }
        return result;
    }

    public void save(AppUser appUser) {
        appUserDao.save(appUser);
    }

    public HashMap<String,Object> getAppUserList(Integer pageNo, Integer pageSize, String search) {
        Pager<List<Map>> appUserPager = appUserDao.getAppUserList(pageNo, pageSize, search);
        return JsonWrapper.successPagerWrapper(appUserPager.getContent(),appUserPager.getRecordTotal());
    }

    //获取所有驾驶证证件到期且未被冻结的顺风车司机
    public List<AppUser> getExpireAppUserList() {
        return appUserDao.getExpireAppUserList();
    }
}
